/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.data;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

import edu.bsu.android.gtv.apps.bconnected.objects.Organization;
import edu.bsu.android.gtv.apps.bconnected.parser.ParserFactory;
import edu.bsu.android.gtv.apps.bconnected.parser.ServiceParser;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;

/**
 * Data layer for the {@link Organization} object
 * @author Kyle Parker, University Computing Services
 */
public class OrganizationData {
    private static ArrayList<Organization> mOrganizations;
    private static ArrayList<Organization> mSharedOrganizations;
    private static Organization mOrganization;

    /**
     * Loads an ArrayList<Organization> from the service handler containing the list of organizations for a student based on the 
     * BSU ID and the category ID.  The list will contain one of the following three groups of organization data:
     * Organizations the student is currently a member of
     * Organizations related to the degree, but that the student may or may not be a member of
     * All organizations based on the category ID  
     * @param bsuID
     * @param categoryID
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     * @throws UnsupportedEncodingException
     */
    public static void initializeOrganization(String bsuID, int categoryID, Activity activity) 
    		throws UnsupportedEncodingException {
    	List<String> querystringParms = new ArrayList<String>(3);
    	
    	if (categoryID == 900) {    		
        	// Retrieve the "degree organizations" based on the BSU ID
    		querystringParms.add(Constants.QUERYSTRING_QUERY_ID + Constants.QUERY_ORGANIZATION_DEGREE); 
    		querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8"));     			
    	} else if (categoryID == 800) {
        	// Retrieve the list of organizations for the student based on the BSU ID and the term
    		querystringParms.add(Constants.QUERYSTRING_QUERY_ID + Constants.QUERY_ORGANIZATION_MEMBER); 
    		querystringParms.add(Constants.QUERYSTRING_BSU_ID +  URLEncoder.encode(bsuID, "UTF-8"));     			
    	} else {
        	// Retrieve the list based on the category ID
    		querystringParms.add(Constants.QUERYSTRING_QUERY_ID + Constants.QUERY_ORGANIZATION_CATEGORY); 
    		querystringParms.add(Constants.QUERYSTRING_CATEGORY_ID + Integer.toString(categoryID));  
    	}

		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_ORGANIZATION, querystringParms, activity);
		mOrganizations = parser.parseOrganization();
    }

    /**
     * Loads an ArrayList<Organization> from the service handler containing the list of shared organizations for a student and another 
     * member based on the BSU IDs 
     * @param bsuID
     * @param memberBSUID
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     * @throws UnsupportedEncodingException
     */
    public static void initializeSharedOrganization(String bsuID, String memberBSUID, Activity activity) 
    		throws UnsupportedEncodingException {
    	List<String> querystringParms = new ArrayList<String>(4);
    	
		querystringParms.add(Constants.QUERYSTRING_QUERY_ID + Constants.QUERY_ORGANIZATION_SHARED); 
		querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8"));
		querystringParms.add(Constants.QUERYSTRING_MEMBER_BSU_ID +  URLEncoder.encode(memberBSUID, "UTF-8"));

		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_ORGANIZATION, querystringParms, activity);
		mSharedOrganizations = parser.parseOrganization();
    }

    /**
     * Loads an Organization object from the service handler containing the organization detail based on the ID
     * @param bsuID
     * @param memberBSUID
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     */
    public static void initializeOrganization(String orgID, Activity activity) {
    	List<String> querystringParms = new ArrayList<String>(4);
    	
		querystringParms.add(Constants.QUERYSTRING_QUERY_ID + Constants.QUERY_ORGANIZATION_ID); 
		querystringParms.add(Constants.QUERYSTRING_ORG_ID + orgID);

		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_ORGANIZATION_INFO, querystringParms, activity);
		ArrayList<Organization> org = parser.parseOrganization();
		
		if (org != null) {
			mOrganization = org.get(0);
		}
    }

    /**
     * Returns the integer position of a Organization object within the ArrayList<Organization> based on a specific organization ID
     * @param id
     * @return i|0
     */
    public static int getOrganizationByID(String id) {
		for (int i = 0; i < mOrganizations.size(); i++) {
	    	if (mOrganizations.get(i).getOrganizationID().equals(id)) {
	    		return i;
	    	}
        }
        
        return 0;
    }

    /**
     * Returns the integer value of the organization object size
     * @return mOrganizations.size() | 0
     */
    public static int getOrganizationCount() {
    	if (mOrganizations == null) {
    		return 0;
    	} else {
            return mOrganizations.size();
    	}
    }

    /**
     * Returns a organization object at the index specified
     * @param i - the index of the object to be returned
     * @return mOrganizations.get(i)
     */
    public static Organization getOrganization(int i) {
        return mOrganizations.get(i);
    }

    /**
     * Returns a organization object
     * @return mOrganization
     */
    public static Organization getOrganization() {
        return mOrganization;
    }

    /**
     * Returns the ArrayList<Organization>
     * @return mOrganizations
     */
    public static ArrayList<Organization> getOrganizations() {
        return mOrganizations;
    }

    /**
     * Returns the integer value of the organization object size
     * @return mSharedOrganizations() | 0
     */
    public static int getSharedOrganizationCount() {
    	if (mSharedOrganizations == null) {
    		return 0;
    	} else {
            return mSharedOrganizations.size();
    	}
    }

    /**
     * Returns a organization object at the index specified
     * @param i - the index of the object to be returned
     * @return mSharedOrganizations(i)
     */
    public static Organization getSharedOrganization(int i) {
        return mSharedOrganizations.get(i);
    }

    /**
     * Returns the ArrayList<Organization>
     * @return mSharedOrganizations
     */
    public static ArrayList<Organization> getSharedOrganizations() {
        return mSharedOrganizations;
    }
}
